package dao;

import logic.Navigator;
import logic.Pilot;
import org.hibernate.Session;
import util.HibernateUtil;

import java.util.List;

public class NavigatorDAOImpl {

    private Session sessionFactory;

    public void addNavigator(Navigator navigator) {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        sessionFactory.save(navigator);
        sessionFactory.getTransaction().commit();
        sessionFactory.close();
    }

    public List<Navigator> listNavigator() {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        List<Navigator> navigators = sessionFactory.createCriteria(Navigator.class).list();
        sessionFactory.getTransaction().commit();
        sessionFactory.close();

        return navigators;
    }

    public List<Navigator> getNavigatorById(int navigatorID) {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        List<Navigator> navigator = sessionFactory.createSQLQuery("select * from Navigators where id = " + navigatorID ).addEntity(Navigator.class).list();
        sessionFactory.getTransaction().commit();
        sessionFactory.close();

        return navigator;
    }

    public void removeNavigator(Integer id) {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        Navigator navigator = (Navigator) sessionFactory.load(Navigator.class, id);
        if (navigator != null) {
            sessionFactory.delete(navigator);
        }
        sessionFactory.getTransaction().commit();
        sessionFactory.close();
    }
}
